﻿using System;
using System.Diagnostics;
using static SmartSite.Common.CheckingNullArgument;
using static SmartSite.Frame.FrameTypeEnum;

namespace SmartSite.Frame
{
    /// <summary>
    /// 身份验证请求帧
    /// </summary>
    public class IdentityReqFrame : WithMonitorDeviceSnFrame
    {
        readonly FrameTypeEnum _Key = IdentityReqFrameType;
        public override FrameTypeEnum Key { get { return _Key; } }

        /// <summary>
        /// 用户名. 32 bytes. 紧邻 MonitorDeviceId 之后.
        /// </summary>
        readonly IValueField<string> UserNameField;
        public string UserName
        {
            get => UserNameField.Value;
            set => UserNameField.Value = value;
        }

        /// <summary>
        /// 密码. 32 bytes. 紧邻 UserName 之后.
        /// </summary>
        readonly IValueField<string> PWDField;
        public string PWD
        {
            get => PWDField.Value;
            set => PWDField.Value = value;
        }


        public IdentityReqFrame()
        : this(Array.Empty<byte>()) => InitFieldValue();

        protected IdentityReqFrame(byte[] buffer)
        : base(CheckingNull(buffer, nameof(buffer)))
        {
            UserNameField = new StringField(MonitorDeviceSnField, 32 << 3);
            PWDField = new StringField(UserNameField, 32 << 3);
            CRCField = new UShortField(PWDField);
        }

        new void InitFieldValue()
        {
            base.InitFieldValue();
            FrameType = _Key;
            InfoLength = EvalInfoLength(); // 因为在构造函数中使用，所以不能使用虚方法
            Debug.Assert(InfoLength == 64);
        }
    }
}
